package com.iqiyi.pps.epg.core.service.news;

import com.iqiyi.pps.epg.api.model.web.news.TNewsDetailsData;
import com.iqiyi.pps.epg.api.model.web.news.TNewsDetailsDataPage;
import com.iqiyi.pps.epg.api.model.web.news.TStatisticNewsDetailsData;
import com.iqiyi.pps.epg.api.model.web.news.TStatisticNewsDetailsDataPage;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by zhang on 2016/12/6.
 */
@Service
@Transactional
public class StatisticNewsDetailsDataServiceImpl implements StatisticNewsDetailsDataService {

    public static Logger logger = LoggerFactory.getLogger(StatisticNewsDetailsDataServiceImpl.class);

    @Override
    public TStatisticNewsDetailsDataPage statisticNewsDetailsData(TNewsDetailsDataPage tNewsDetailsDataPage) {
        if (tNewsDetailsDataPage == null) {
            return null;
        }
        TStatisticNewsDetailsDataPage tStatisticNewsDetailsDataPage = new TStatisticNewsDetailsDataPage();
        List<TStatisticNewsDetailsData> tStatisticNewsDetailsDatas = new ArrayList<>();

        Map<String, TStatisticNewsDetailsData> maps = new HashMap<>();
        List<TNewsDetailsData> tNewsDetailsDatas = tNewsDetailsDataPage.getResult();
        TPage tPage = tNewsDetailsDataPage.getTPage();
        for(TNewsDetailsData tNewsDetailsData : tNewsDetailsDatas) {
            String productTime = tNewsDetailsData.getProductTime().substring(0, 10);
            if (maps.containsKey(productTime)) {
                TStatisticNewsDetailsData tStatisticNewsDetailsData = maps.get(productTime);
                tStatisticNewsDetailsData.setAvgPercentTitle(tStatisticNewsDetailsData.getAvgPercentTitle() + tNewsDetailsData.getPercentTitle());
                tStatisticNewsDetailsData.setErrorWordNum(tStatisticNewsDetailsData.getErrorWordNum() + tNewsDetailsData.getErrorWordNum());
                tStatisticNewsDetailsData.setCorrectWordNum(tStatisticNewsDetailsData.getCorrectWordNum() + tNewsDetailsData.getCorrectWordNum());
                tStatisticNewsDetailsData.setAvgErrorStartNum(tStatisticNewsDetailsData.getAvgErrorStartNum() + tNewsDetailsData.getErrorStartNum());
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorStartNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectStartNum(tStatisticNewsDetailsData.getCorrectStartNum() + 1);        //识别入点正确（已发布且入点误差为0）
                }
                tStatisticNewsDetailsData.setTotalStartNum(tStatisticNewsDetailsData.getTotalStartNum() + 1);
                tStatisticNewsDetailsData.setAvgErrorEndNum(tStatisticNewsDetailsData.getAvgErrorEndNum() + tNewsDetailsData.getErrorEndNum());
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorEndNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectEndNum(tStatisticNewsDetailsData.getCorrectStartNum() + 1);        //识别出点正确（已发布且出点误差为0）
                }
                tStatisticNewsDetailsData.setTotalEndNum(tStatisticNewsDetailsData.getTotalStartNum() + 1);
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorEndNum() == 0  && tNewsDetailsData.getErrorStartNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectNewsNum(tStatisticNewsDetailsData.getCorrectNewsNum() + 1);
                }
                tStatisticNewsDetailsData.setTotalNewsNum(tStatisticNewsDetailsData.getTotalNewsNum() + 1);
            } else {
                TStatisticNewsDetailsData tStatisticNewsDetailsData = new TStatisticNewsDetailsData();
                tStatisticNewsDetailsData.setNewsName(tNewsDetailsData.getNewsName());
                tStatisticNewsDetailsData.setChannelName(tNewsDetailsData.getChannelName());
                tStatisticNewsDetailsData.setServerRegion(tNewsDetailsData.getServerRegion());
                tStatisticNewsDetailsData.setServerIp(tNewsDetailsData.getServerIp());
                tStatisticNewsDetailsData.setProductTime(productTime);
                tStatisticNewsDetailsData.setCostTime(tNewsDetailsData.getCostTime());
                tStatisticNewsDetailsData.setAvgPercentTitle(tNewsDetailsData.getPercentTitle());
                tStatisticNewsDetailsData.setErrorWordNum(tNewsDetailsData.getErrorWordNum());
                tStatisticNewsDetailsData.setCorrectWordNum(tNewsDetailsData.getCorrectWordNum());
                tStatisticNewsDetailsData.setAvgErrorStartNum(tNewsDetailsData.getErrorStartNum());
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorStartNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectStartNum(1);        //识别入点正确（已发布且入点误差为0）
                } else {
                    tStatisticNewsDetailsData.setCorrectStartNum(0);
                }
                tStatisticNewsDetailsData.setTotalStartNum(1);
                tStatisticNewsDetailsData.setAvgErrorEndNum(tNewsDetailsData.getErrorEndNum());
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorEndNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectEndNum(1);        //识别出点正确（已发布且出点误差为0）
                } else {
                    tStatisticNewsDetailsData.setCorrectEndNum(0);
                }
                tStatisticNewsDetailsData.setTotalEndNum(1);
                if (tNewsDetailsData.getStatus() == 1 && tNewsDetailsData.getErrorEndNum() == 0  && tNewsDetailsData.getErrorStartNum() == 0) {
                    tStatisticNewsDetailsData.setCorrectNewsNum(1);
                } else {
                    tStatisticNewsDetailsData.setCorrectNewsNum(0);
                }
                tStatisticNewsDetailsData.setTotalNewsNum(1);

                maps.put(productTime, tStatisticNewsDetailsData);
            }
        }

        int max = tNewsDetailsDatas.size();
        for (Iterator<String> keys = maps.keySet().iterator(); keys.hasNext();) {
            String key = keys.next();
            TStatisticNewsDetailsData tStatisticNewsDetailsData = maps.get(key);
            tStatisticNewsDetailsData.setAvgPercentTitle(tStatisticNewsDetailsData.getAvgPercentTitle() / max);
            tStatisticNewsDetailsData.setAvgErrorStartNum(tStatisticNewsDetailsData.getAvgErrorStartNum() / max);
            tStatisticNewsDetailsData.setAvgErrorEndNum(tStatisticNewsDetailsData.getAvgErrorEndNum() / max);
            tStatisticNewsDetailsDatas.add(tStatisticNewsDetailsData);
        }

        tStatisticNewsDetailsDataPage.setTPage(tPage);
        tStatisticNewsDetailsDataPage.setResult(tStatisticNewsDetailsDatas);
        return tStatisticNewsDetailsDataPage;
    }
}
